<?php

defined('ACC')||exit('Access Denied');

class GoodsModel extends Model{
	
	/* 添加一行商品 */
	public function add($data){
		if($this->db->autoExecute($data,'goods')){
			return $this->db->last_id();
		}
		return false;
	}
	
	/* 修改一行商品 */
	public function update($data,$where){
		if($this->db->autoExecute($data,'goods','update',$where)){
			return true;
		}
		return false;
	}
	
	/* 获取商品列表 */
	public function getList($pageNow, $pageSize){
		$sql='select goods_id,goods_name,shop_price,is_on_sale,is_best,is_new,is_hot,goods_number,article_number from goods where is_delete=0 order by goods_id asc  limit '.(($pageNow-1)*$pageSize).','.$pageSize;
		return $this->db->getAll($sql);
	}
	
	/* 获取商品总行数 */
	public function getRowCount(){
		$sql = 'select count(*) from goods';
		return $this->db->getOne($sql);
	}
	
	/* 交易失败，回滚 */
	public function rollback($goods_id){
		$sql = 'update goods set is_delete=1 where goods_id='.$goods_id;
		if($this->db->query($sql)){
			return $this->db->affected_rows();
		}
		return false;
	}
	
	/* 删除一条商品 */
	public function delete($goods_id){
		$sql = "delete from goods where goods_id = $goods_id";
		return $this->db->query($sql);
	}
	
	/* 获取最热，最新，最好的商品 */
	public function getTop($type=''){
		if($type!='best' && $type!='hot' && $type!='new'){
			return false;
		}
		$sql = 'select goods_id,goods_name,market_price,shop_price,goods_thumb from goods where is_delete=0 and is_'.$type.'=1 limit 3';
		return $this->db->getAll($sql);
	}
	
	/* 获取一行商品信息 */
	public function detail($goods_id){
		$sql = 'select * from goods where is_delete=0 and goods_id='.$goods_id;
		return $this->db->getRow($sql);
	}
	
	/* 获取指定栏目下的商品信息 */
	public function catGoods($arr){
		$in='('.implode(',',$arr).')';

		$sql = 'select goods_id,mername,goods_name,goods_thumb,shop_price,market_price from goods left join mer on goods.mer_id=mer.mer_id where cat_id in '.$in.' limit 6';

		return $this->db->getAll($sql);
	}
	
	/* 获取商品名 */
	public function getGoodName($goods_id){
		$sql = "select goods_name from goods where goods_id = $goods_id";
		return $this->db->getOne($sql);
	}
	
	/* 获取一行商品信息 */
	public function getRow($goods_id){
		$sql = "select * from goods where goods_id = '$goods_id'";
		return $this->db->getRow($sql);
	}
	
	/* 获取商家列表 */
	public function getMerList(){
		$sql = 'select mer_id, mername from mer';
		return $this->db->getAll($sql);
	}
	
	/* 获取指定条件下的商品总行数 */
	public function getRowCount2($where){
		$sql = "select count(*) from (select goods_id from goods where $where) as temp order by goods_id asc";
		return $this->db->getOne($sql);
	}
	
	/* 获取商品列表 */
	public function getList2($pageNow, $pageSize, $where){
		$sql='select * from (select goods_id,goods_name,shop_price,is_on_sale,is_best,is_new,is_hot,goods_number,article_number from goods where ' . $where .' and is_delete=0) as temp order by goods_id asc  limit '.(($pageNow-1)*$pageSize).','.$pageSize;
		return $this->db->getAll($sql);
	}
	
	/* 获取某栏目下的所有属性 */
	public function get_attr_list($cat_id){
		$sql = 'select attr_id, cat_id, attr_name, attr_input_type, attr_values from attribute where cat_id = ' . $cat_id;
		return $this->db->getAll($sql);
	}
	
	/* 获取某商品的栏目id */
	public function get_cat_id($goods_id){
		$sql = 'select cat_id from goods where goods_id = ' . $goods_id;
		return $this->db->getOne($sql);
	}
	
	/* 获取某一栏目信息 */
	public function get_cat_row($cat_id){
		$sql = 'select cat_id, cat_name from category where cat_id = ' . $cat_id;
		return $this->db->getRow($sql);
	}
	
	/* 获取栏目列表 */
	public function get_cat_list(){
		$sql = 'select cat_id, cat_name from goods_type';
		return $this->db->getAll($sql);
	}
	
	/* 获取指定商品，指定属性下的属性id */
	public function get_attr_id($attr_id, $goods_id){
		$sql = 'select goods_attr_id from goods_attr where attr_id = ' . $attr_id . ' and goods_id = ' . $goods_id;
		return $this->db->getOne($sql);
	}
	
	/* 添加一个商品属性 */
	public function goods_attr_insert($data, $table){
		return $this->db->autoExecute($data, $table);
	}
	
	/* 修改商品属性 */
	public function goods_attr_update($data, $table, $act, $where){
		return $this->db->autoExecute($data, $table, $act, $where);
	}
	
	/* 获取图片路径 */
	public function get_img_path($goods_id){
		$sql = 'select goods_thumb, ori_img from goods where goods_id = ' . $goods_id;
		return $this->db->getRow($sql);
	}
	
	// ********
	
	/* 获取一行商品信息 */
	public function get_good_info($good_id){
		$sql = 'select goods_id, goods_name, article_number, goods_number, mer.mername, goods_number, market_price, shop_price, goods_desc, goods_thumb, add_time from goods left join mer on goods.mer_id = mer.mer_id where goods_id = ' . $good_id;
		return $this->db->getRow($sql);
	}
	
	/* 获取某一商品下的所有商品属性 */
	public function get_good_attrs($good_id){

		$sql = 'select attr_id from goods_attr where goods_id = ' . $good_id;
		if($attr_ids = $this->db->getAll($sql)){
			foreach($attr_ids as $key => $val){
			$sql = 'select attr_name, attr_value from attribute left join goods_attr on attribute.attr_id = goods_attr.attr_id where attribute.attr_id = ' . $val['attr_id'] . ' and goods_id = ' . $good_id;
				if($attr = $this->db->getRow($sql)){
					$attrs[] = $attr;
				}else{
					return false;
				}
			}
			return $attrs;
		}
		
	}
	
	// ur_here 获取某商品下对应的栏目信息
	public function get_cat_info($goods_id){
		$sql = 'select cat_id, cat_name from category where  cat_id in ( select cat_id from goods where goods_id = ' . $goods_id . ')';
		return $this->db->getRow($sql);
	}
	
	// 获取某栏目的父类栏目id
	public function get_parent($cat_id){
		$sql = 'select cat_id, cat_name from category where cat_id in (select parent_id from category where cat_id = ' . $cat_id . ')';
		return $this->db->getRow($sql);
	}
	// ---
	
	/* 获取商品列表，并排序 */
	public function get_goods_list($sort){
		$sql = 'select goods_id, goods_name, market_price, shop_price, goods_thumb from goods order by ' . $sort[0] . ' ' . $sort[1];
		return $this->db->getAll($sql);
	}
	
	/* 获取指定商品的商品信息 */
	public function get_goods($ids){
		foreach($ids as $key => $val){
			$val = $val + 0;
		}
		$str = join(',', $ids);
		
		$sql = 'select goods_id, goods_name from goods where goods_id in (' . $str . ')';
		return $this->db->getAll($sql);
	}

}








?>